package values

import (
	"gitee.com/zhongguo168a/gocodes/datax"
	"gitee.com/zhongguo168a/gocodes/datax/convertx"
	"gitee.com/zhongguo168a/gocodes/datax/mapx"
	"strconv"
)

func NewValuesMap() (obj *ValuesMap) {
	obj = &ValuesMap{}
	obj.tableDictOnKV = map[string]map[string]interface{}{}
	obj.tableDictOnList = map[string][]interface{}{}
	return
}

type ValuesMap struct {
	// 表名->编号->行
	tableDictOnKV map[string]map[string]interface{}
	// 表名->行列表
	tableDictOnList map[string]datax.A
}

func (values *ValuesMap) ParseMap(fileDict datax.M) {
	tableDictOnKV := values.tableDictOnKV
	tableDictOnList := values.tableDictOnList
	for fileKey, ifileDict := range fileDict {
		if ifileDict == nil {
			continue
		}
		sheetMap := ifileDict.(datax.M)

		for sheetKey, sheetDict := range sheetMap {
			ident := fileKey + "/" + sheetKey
			//
			tableOnKV, has := tableDictOnKV[ident]
			if !has {
				tableOnKV = datax.M{}
				tableDictOnKV[ident] = tableOnKV
			}
			tableOnList, has := tableDictOnList[ident]
			if !has {
				tableOnList = datax.A{}
			}
			//
			if sheetDict != nil {
				rows := sheetDict.([]interface{})
				titles := func() (x []string) {
					irow := rows[0]
					row := irow.(datax.A)
					x = make([]string, len(row))
					for index, rowVal := range row {
						x[index] = convertx.AnyToString(rowVal)
					}
					return
				}()
				types := func() (x []string) {
					irow := rows[1]
					row := irow.(datax.A)
					x = make([]string, len(row))
					for index, rowVal := range row {
						x[index] = convertx.AnyToString(rowVal)
					}
					return
				}()
				rows = rows[2:]
				for _, irow := range rows {
					row := irow.(datax.A)
					rowMap := datax.M{}
					for col, rowVal := range row {
						title := titles[col]
						typ := types[col]
						switch typ {
						case "float64":
							rowMap[title] = convertx.AnyToFloat64(rowVal)
						case "int":
							rowMap[title] = convertx.AnyToInt(rowVal)
						case "string":
							rowMap[title] = convertx.AnyToString(rowVal)
						case "bool":
							rowMap[title] = convertx.AnyToBool(rowVal)
						default:
							rowMap[title] = rowVal
						}
					}

					编号 := mapx.String(rowMap, "编号")
					tableOnKV[编号] = rowMap
					tableOnList = append(tableOnList, rowMap)
				}

				tableDictOnList[ident] = tableOnList
			}
		}
	}
}

func (values *ValuesMap) GetByIdent(表名, 编号 string) datax.M {
	tableDict := values.tableDictOnKV
	table, has := tableDict[表名]
	if !has {
		return nil
	}
	row, has := table[编号]
	if !has {
		return nil
	}
	return row.(datax.M)
}

func (values *ValuesMap) GetByField(表名, 字段 string, 值 string) datax.M {
	tableDict := values.tableDictOnKV
	table, has := tableDict[表名]
	if !has {
		return nil
	}
	v, has := table[字段]
	if !has {
		return nil
	}
	if v != 值 {
		return nil
	}
	return v.(datax.M)
}

func (values *ValuesMap) GetBySection(表名, 字段 string, 值 int) datax.M {
	dataList := values.tableDictOnList
	m, has := dataList[表名]
	if !has {
		return nil
	}

	for _, val := range m {
		valMap := val.(datax.M)
		字段值 := mapx.Int(valMap, 字段)
		if 值 <= 字段值 {
			return valMap
		}
	}

	return nil
}

func (values *ValuesMap) ListByField(表名, 字段 string) datax.M {
	tableDict := values.tableDictOnList
	table, has := tableDict[表名]
	if !has {
		return nil
	}

	result := datax.M{}
	for i, irow := range table {
		row := irow.(datax.M)
		result[strconv.Itoa(i)] = row[字段]
	}

	return result
}

func (values *ValuesMap) LengthByTable(表名 string) int {
	tableDict := values.tableDictOnList
	table, has := tableDict[表名]
	if !has {
		return 0
	}
	return len(table)
}
